<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改菜单')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body class="white-bg">
<div class="modal inmodal fade" id="myModal6" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span>
                </button>
                <h4 class="modal-title">选择岗位及调岗时间</h4>
            </div>
            <div class="modal-body">
                <p>
                    <input type="text" name="effectdate" class="form-control" id="eVeffectdate" placeholder="请选择调岗生效日期"
                           required>
                    <br/>
                    <select id="job" name='jobID' class="form-control">
                    </select>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" id="missmodal" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" onclick="obtain()" class="btn btn-primary">确认</button>
            </div>
        </div>
    </div>
</div>


<section class="section-content">
    <div class="row">
        <div class="col-sm-9">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title ibox-title-gray dashboard-header gray-bg">
                        <h5>人员信息</h5>
                    </div>
                    <div class="ibox-content h-200">
                        <button class="btn btn-primary btn-sm" type="button" onclick="confirmex()"><i
                                class="fa fa-check"></i>&nbsp;调换
                        </button>
                        <button class="btn btn-warning btn-sm" type="button" onclick="Reset()"><i
                                class="fa fa-refresh"></i>&nbsp;刷新
                        </button>
                        <button class="btn btn-success btn-sm" type="button" onclick="recovery()"><i
                                class="fa fa-arrows-h"></i>&nbsp;恢复
                        </button>
                        <button type="button" id="modal" class="btn btn-primary" data-toggle="modal"
                                data-target="#myModal6">小模态窗口
                        </button>

                        <form id="emp-form">
                            <input type="hidden" name="comp" id="comp">
                            <input type="hidden" name="department" id="department">
                            <input type="hidden" name="jobs" id="jobs">
                            <input type="hidden" name="compID" id="compID">
                            <input type="hidden" name="departmentID" id="departmentID">
                            <input type="hidden" name="jobsID" id="jobsID">
                            <div class="table-striped">
                                <table id="emp-table" data-mobile-responsive="true"></table>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title ibox-title-gray dashboard-header gray-bg">
                        <h5>岗位信息</h5>
                    </div>
                    <div class="ibox-content h-200">
                        <form id="exchange-form">
                            <input type="hidden" name="eid" id="eid">
                            <div class="table-striped">
                                <table id="change-table" data-mobile-responsive="true"></table>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title ibox-title-gray dashboard-header gray-bg">
                    <h5>组织架构</h5>
                    <div class="box-tools pull-right">
                        <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开"
                                style="display:none;">
                            <i class="fa fa-chevron-up"></i></button>
                        <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                                class="fa fa-chevron-down"></i></button>
                        <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i
                                class="fa fa-refresh"></i></button>
                    </div>
                </div>
                <div class="ui-layout-content">
                    <div id="tree" class="ztree"></div>
                </div>
            </div>
        </div>
    </div>
    </form>
</section>


<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: datetimepicker-js"/>


<script th:inline="javascript">
    var prefix = ctx + "jinpeng";
    var detailFlag = [[${@permission.hasPermi('hr:exchange:detail')}]];
    $(function () {
        $("#modal").hide();
        queryDeptTree();
        queryUserList();
        changeList();
        $("#eVeffectdate").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    });

    function queryDeptTree() {
        var url = ctx + "jinpeng/treeData?type=1";
        var options = {
            url: url,
            expandLevel: 2,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            var row = $("#emp-table").bootstrapTable('getSelections');
            if (row.length == 0) {
                $.modal.alertWarning('请选择需要转岗的人员!')
            } else {
                if (treeNode.id > 30000) {
                    obtain(null, null, treeNode.id - 30000, treeNode.title, null, null);
                }
            }
        }
    }

    function getJob(departID) {
        var html = "";
        $.ajax({
            async: false,
            url: "/empentry/findJob",
            data: {
                "DepID": departID
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#job").empty();
                var arry = data;
                for (var i = 0; i < arry.length; i++) {
                    html += "<option value='"
                        + arry[i].jobID
                        + "'>"
                        + arry[i].title
                        + "</option>";
                }
                $("#job").append(html);
                $("#modal").click();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert("错误,请联系管理员,错误信息:" + textStatus);
            }
        });
    }

    function obtain(compID, compName, departID, depatName, jobID, jobName) {
        //如果部门ID部位空，公司和岗位ID都是空，说明是点击组织架构进入的
        if (departID != null && compID == null && jobID == null) {
            $("#department").val(depatName);
            $("#departmentID").val(departID);
            alert(departID);
            $.ajax({
                async: false,
                url: ctx + "company/info/findCompanyByDepId",
                data: {
                    "depID": departID
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    $("#comp").val(data.title);
                    $("#compID").val(data.compID);
                    getJob(departID);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                    alert("错误,请联系管理员,错误信息:" + textStatus);
                }
            });
        } else {
            $("#jobs").val($("#job option:checked").text());
            $("#jobsID").val($("#job").val());
            var eVeffectdate = $("#eVeffectdate").val();
            var compName = $("#comp").val();
            var departmentName = $("#department").val();
            var jobName = $("#jobs").val();
            var compID = $("#compID").val();
            var departmentID = $("#departmentID").val();
            var jobID = $("#jobsID").val();
            var row = $("#emp-table").bootstrapTable('getSelections');
            for (var i = 0; i < row.length; i++) {
                $("#emp-table").bootstrapTable('updateRow',
                    row[i].eVcompname = compName, row[i].eVdepartname = departmentName,
                    row[i].eVjobname = jobName, row[i].eVcompID = compID, row[i].eVdepartID = departmentID,
                    row[i].eVjobID = jobID, row[i].eVeffectdate = eVeffectdate);
            }
            $("#missmodal").click();
        }
    }

    function changeList() {
        var options = {
            url: prefix + "/EeventList?type=All",
            id: "change-table",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove/{id}",
            escape: true,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            columns: [
                {
                    field: 'eName',
                    title: '姓名',
                },
                {
                    field: 'remarks',
                    title: '类型',
                },
                {
                    field: 'effectDate',
                    title: '生效日期'
                },
                {
                    field: 'compname',
                    title: '公司名称'
                },
                {
                    field: 'depname',
                    title: '部门名称'
                },
                {
                    field: 'jobname',
                    title: '岗位名称'
                },
                {
                    field: 'reportname',
                    title: '上级领导'
                }
            ]
        };
        $.table.init(options);
    }

    function queryUserList() {
        var options = {
            id: "emp-table",
            code: "eid",
            uniqueId: "eid",
            url: prefix + "/exchangeEmp?eid=" + [[${eid}]],
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove/{id}",
            escape: true,
            showPageGo: true,
            rememberSelected: true,
            pagination: false,
            showColumns: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            modalName: "基本信息",
            columns: [{
                checkbox: true,
            }, {
                field: 'badge',
                title: '员工编号',
            },
                {
                    field: 'name',
                    title: '姓名'
                },
                {
                    field: 'compname',
                    title: '当前公司'
                },
                {
                    field: 'compID',
                    visible: false
                },
                {
                    field: 'departname',
                    title: '当前部门'
                },
                {
                    field: 'depID',
                    visible: false
                },
                {
                    field: 'jobname',
                    title: '当前岗位'
                },
                {
                    field: 'jobID',
                    visible: false
                },
                {
                    field: 'eVcompname',
                    title: '调整公司'
                },
                {
                    field: 'eVdepartname',
                    title: '调整部门'
                },
                {
                    field: 'eVjobname',
                    title: '调整岗位'
                },
                {
                    field: 'eVeffectdate',
                    title: '生效日期'
                },
                {
                    field: 'eVcompID',
                    visible: false
                },
                {
                    field: 'eVdepartID',
                    visible: false
                },
                {
                    field: 'eVjobID',
                    visible: false
                }
                , {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="xiangxi(\'' + row.eid + '\')"><i class="fa fa-search"></i>调岗详情</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }

    function confirmex() {
        var data = $("#emp-table").bootstrapTable('getSelections');
        if (data.length == 0) {
            $.modal.alertWarning('请选择一项!');
            return;
        } else {
            $.modal.confirm("确认要进行调岗吗?", function () {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].eVcompname == "" || data[i].eVcompname == null) {
                        $.modal.alertWarning('存在未选择调换岗位的员工!');
                        return;
                    }
                }
                $.ajax({
                    async: false,
                    url: "/jinpeng/exchange",
                    data: JSON.stringify(data),
                    type: "post",
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                        $.modal.alertSuccess('岗位调换成功')
                        $("#emp-table").bootstrapTable('refresh');
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

                        alert("错误,请联系管理员,错误信息:" + textStatus);
                    }
                });
            });
        }
    }

    function xiangxi(eid) {
        $("#eid").val(eid);
        $.table.search("exchange-form");
    }

    function Reset() {
        $("#emp-table").bootstrapTable('refresh');
    }

    function recovery() {
        var row = $("#emp-table").bootstrapTable('getSelections');
        var allEid = "";
        if (row.length == 0) {
            alert("请选择一项");
            return;
        }
        for (var i = 0; i < row.length; i++) {
            if (i == row.length - 1) {
                allEid += row[i].eid
            } else {
                allEid += row[i].eid + ","

            }
        }
        alert(allEid);
        $.ajax({
            async: false,
            url: "/jinpeng/recoveryEx",
            data: {"allEid": allEid},
            type: "post",
            dataType: "json",
            success: function (data) {
                $.modal.alertSuccess('成功恢复' + data + "人的岗位")
                $("#emp-table").bootstrapTable('refresh');
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("错误,请联系管理员,错误信息:" + textStatus);
            }
        });
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });
    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });
    $('#btnRefresh').click(function () {
        queryDeptTree();
    });
</script>
</body>
</html>
